import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/Xwidgets/ad_box.dart';
import 'package:yp/config/index.dart';
import 'package:yp/config/style.dart';
import 'package:yp/router.dart';
import 'package:yp/services/api.dart';
import 'package:yp/widgets/button_widget.dart';
import 'package:yp/widgets/index.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/// 消费有礼
class ConsumeGift extends StatefulWidget {
  const ConsumeGift({Key key}) : super(key: key);

  @override
  _ConsumeGiftState createState() => _ConsumeGiftState();
}

class _ConsumeGiftState extends State<ConsumeGift> {
  List consumeNameList = [];
  List consumeList = [];
  List bannerList = [];

  /*
 award_type 601, "生日有礼"
award_type 603, "完善信息有礼"
award_type 604, "评价有礼"
award_type 605, "首单有礼"
award_type 606, "消费有礼"
award_type 607, "分享链接有礼"
award_type 608, "储值有礼"
award_type 610, "注册有礼"
    */
  getConsume() {
    var params = {
      "channel_num_id": 99,
      "sub_unit_num_id": 102002,
      // "ec_shop_num_id": SpUtil.getInt('storeShopNum'),
      "ec_shop_num_id": SpUtil.getObject(Config.GET_SHOP_INFO)['shop_id'],
      "award_types": null,
      'access_token': SpUtil.getString('token'),
    };
    Api.taskQuery(params).listen((event) {
      if (event['code'] == 0) {
        setState(() {
          if (event['member_surprise_task_infos'] != null &&
              event['member_surprise_task_infos'].length > 0) {
            event['member_surprise_task_infos'].forEach((element) {
              if (element['member_task_infos'] != null &&
                  element['member_task_infos'].length > 0 &&
                  element['award_type'] == 606) {
                element['member_task_infos'].forEach((elements) {
                  var data = elements;
                  data['name'] = element['award_type_name'];
                  consumeList.add(data);
                });
              }
            });
          }
        });
      }
    });
  }

  getBanber() {
    var params = {
      "data_sign": 0,
      "channel_num_id": 98,
      "use_platform": 3,
      "access_token": SpUtil.getString('token'),
      "banner_type": 2
    };

    Api.activityBanner(params).listen((event) {
      if (event['code'] == 0) {
        setState(() {
          if (event['banner_infos'] != null &&
              event['banner_infos'].length > 0) {
            var data = [];
            event['banner_infos'].forEach((element) {
              data.add({'logo': element['image_url']});
            });
            bannerList = data;
            print(bannerList);
          }
        });
      }
    });
  }

  @override
  void initState() {
    getConsume();
    getBanber();
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: YPNavigationBar('消费有礼'),
        body: SingleChildScrollView(
          child: Column(
            children: [
              Stack(
                children: [
                  Padding(
                    padding: const EdgeInsets.only(bottom: 16),
                    // child: Container(),
                    child: AdBox(
                        aspectRatio: 355 / 145,
                        adKey: 'Adv',
                        height: 145,
                        borderRadius: 0,
                        margin: EdgeInsets.zero,
                        imageList: bannerList),
                  ),
                  Positioned(
                      top: 10.w,
                      right: 0,
                      child: GestureDetector(
                        onTap: () {
                          Get.toNamed(RouteConfig.activityRule);
                        },
                        child: Container(
                            padding: EdgeInsets.fromLTRB(6, 3, 6, 3),
                            margin: EdgeInsets.fromLTRB(6, 3, 0, 6),
                            decoration: BoxDecoration(
                                color: Colors.black.withOpacity(0.7),
                                borderRadius: BorderRadius.only(
                                    topLeft: Radius.circular(10),
                                    bottomLeft: Radius.circular(10))),
                            child: Text('奖励规则',
                                style: TextStyle(
                                    color: EsColors.White,
                                    fontSize: EsSizes.XXS,
                                    fontWeight: EsFontWeights.Normal))),
                      ))
                ],
              ),
              ListView.builder(
                  itemCount: consumeList.length,
                  shrinkWrap: true,
                  physics: NeverScrollableScrollPhysics(),
                  itemBuilder: (context, index) => Container(
                        margin: EdgeInsets.fromLTRB(12, 8, 12, 10),
                        padding: EdgeInsets.only(bottom: 16),
                        decoration: EsDecoration.BoxShadowWithRadius,
                        child: Stack(
                          children: [
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Padding(
                                  padding: const EdgeInsets.only(
                                      top: 16, right: 55, left: 12),
                                  child: Text(
                                      consumeList[index]['activity_name']
                                          .toString(),
                                      style: TextStyle(
                                          color: EsColors.Title,
                                          fontSize: EsSizes.LG,
                                          fontWeight: EsFontWeights.Medium)),
                                ),
                                Padding(
                                  padding:
                                      const EdgeInsets.fromLTRB(12, 8, 12, 8),
                                  child: Text(
                                      '${consumeList[index]['begin_dtme'].toString()} 至 ${consumeList[index]['end_dtme'].toString()}',
                                      style: TextStyle(
                                          color: EsColors.Content,
                                          fontSize: EsSizes.SM,
                                          fontWeight: EsFontWeights.Normal)),
                                ),
                                YpLine(),
                                SizedBox(height: 8),
                                Row(
                                  children: [
                                    Expanded(child: SizedBox()),
                                    EsOutLineButton(
                                      '去下单',
                                      onTap: () {
                                        print('去下单那');
                                        Get.offAllNamed(RouteConfig.main);
                                      },
                                      fontSize: EsSizes.SM,
                                      fontWeight: EsFontWeights.Medium,
                                      bgColor: EsColors.Pink,
                                      textColor: EsColors.White,
                                      padding: EdgeInsets.symmetric(
                                          vertical: 5, horizontal: 20),
                                    ),
                                    SizedBox(width: 12)
                                  ],
                                )
                              ],
                            ),
                            Positioned(
                              right: 0,
                              top: 0,
                              child: Container(
                                padding: EdgeInsets.symmetric(
                                    vertical: 2, horizontal: 4),
                                child: Text(
                                  consumeList[index]['name'].toString(),
                                  style: TextStyle(
                                      color: EsColors.White,
                                      fontSize: EsSizes.XS,
                                      fontWeight: EsFontWeights.Normal),
                                ),
                                decoration: BoxDecoration(
                                  image: DecorationImage(
                                      fit: BoxFit.cover,
                                      image: AssetImage(
                                          'assets/images/acquiring_gift_bg.png')),
                                ),
                              ),
                            )
                          ],
                        ),
                      ))
            ],
          ),
        ));
  }
}
